﻿using System;
using System.Configuration;
using System.Diagnostics;
using System.Globalization;
using Sustainalytics.ClientDW.DataAccessLayer.ESGRatings;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.DataLayer.CompanyHistory;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.Entities;
using Sustainalytics.Entities.ESGRatings;

namespace Sustainalytics.HistoricalData.ComputationTool
{
    class Program
    {
        private static DateTime _dailyRecurringDate;
        private static int _computationMonthNumber;

        static void Main(string[] args)
        {
            
            IRepositoryCollection<CorporateData> dwRep = new RepositoryCollection<CorporateData>(ClientDWConnection.GetConnectionString());
            IRepositoryCollection<CompanyHistoricalDataIndicators> historicalDataRepository = new CompanyHistoricalDataIndicatorsRepository(HistorycalDataConnectionString.GetConnectionString(), "CompanyHistoricalDataIndicators_live"); 
            IReferenceUniversesRepository universeRepository = ReferenceUniversesRepository.GetInstance(ClientDWConnection.GetConnectionString()); 
            IWeightMatricesRepository weightMatrixRepository = WeightMatricesRepository.GetInstance(ClientDWConnection.GetConnectionString()); 
            IRepositoryCollection<ESGProfile> esgProfileRepository = new RepositoryCollection<ESGProfile>(ClientDWConnection.GetConnectionString());
            IRepositoryCollection<IndicatorTemplate> indicatorTemplateRepository=new RepositoryCollection<IndicatorTemplate>(ClientDWConnection.GetConnectionString());
            IRepositoryCollection<CompanyHistoricalPerformance> companyHistoricalPerformanceLast36MonthsRepository = new CompanyPerformanceHistoryRepository(HistorycalDataConnectionString.GetConnectionString(), "CompanyHistoricalPerformance_live");
           // IRepositoryCollection<CompanyHistoryData> companyHistoricalPerformanceRepository = new RepositoryCollection<CompanyHistoryData>(HistorycalDataConnectionString.GetConnectionString());


            var tool = new HistoricalDataSyncTool(dwRep, historicalDataRepository, universeRepository,
                weightMatrixRepository, esgProfileRepository, companyHistoricalPerformanceLast36MonthsRepository,indicatorTemplateRepository);


            _dailyRecurringDate = DateTime.Parse(ConfigurationManager.AppSettings["RefenceDate"], CultureInfo.CreateSpecificCulture("en-US"));

            if (_dailyRecurringDate == DateTime.MinValue)
            {
                _dailyRecurringDate = DateTime.UtcNow.Date;
            }

            if (!int.TryParse(ConfigurationManager.AppSettings["ComputingMonthsNumber"], NumberStyles.Number,
                    CultureInfo.CreateSpecificCulture("en-US"), out _computationMonthNumber))
            {
                _computationMonthNumber = 36;
            }

            var sw = Stopwatch.StartNew();
            Console.WriteLine("Started Computing Data....");

            var data = tool.ComputeHistoryData(_dailyRecurringDate, _computationMonthNumber);
            sw.Stop();
            var message = string.Format(@"Time elapsed {0:hh\:mm\:ss}", sw.Elapsed);
            Console.WriteLine("Ended Computing Data...."+message);

            Console.WriteLine("Started writing Data....");
            tool.WriteHistoricalDataToDatabase(data);
            Console.WriteLine("Ended writing Data....");
           
        }
    }
}
